<?php

class SiteController extends ServiceController
{
	/**
	 * This is the default action that displays the phonebook Flex client.
	 */
	public function actionIndex()
	{
		$this->render('index');
	}
    
    /**
     * This is the action to handle external exceptions.
     */
    public function actionError()
    {
        if($error=Yii::app()->errorHandler->error)
        {
            if(Yii::app()->request->isAjaxRequest)
                echo $error['message'];
            else
                $this->render('error', $error);
        }
    }

	/**
	 * Returns all contact records.
	 * @return Contact[] the contact records
	 * @soap
	 */
	public function getContacts()
	{
		return Contact::model()->findAll();
	}

	/**
	 * Updates or inserts a contact.
	 * If the ID is null, an insertion will be performed;
	 * Otherwise it updates the existing one.
	 * @param Contact contact model
	 * @return boolean whether saving is successful
	 * @soap
	 */
	public function saveContact($contact)
	{
		if($contact->id > 0) // update
		{
			$contact->isNewRecord=false;
			if(($oldContact=Contact::model()->findByPk($contact->id))!==null)
			{
				$oldContact->attributes=$contact->attributes;
				return $oldContact->save();
			}
			else
				return false;
		}
		else // insert
		{
			$contact->isNewRecord=true;
			$contact->id=null;
			return $contact->save();
		}
	}

	/**
	 * Deletes the specified contact record.
	 * @param integer ID of the contact to be deleted
	 * @return integer number of records deleted
	 * @soap
	 */
	public function deleteContact($id)
	{
		return Contact::model()->deleteByPk($id);
	}
}
